<template>
  <div class="ui-update" style="position: relative;width: 82px;">
    <el-upload
      class="avatar-uploader fx-r fx-ac fx-h"
      :action="upUrl"
      :data="updata"
      :show-file-list="false"
      :headers="headers"
      :on-success="handleAvatarSuccess"
      :on-error="handleAvatarError"
      :before-upload="beforeAvatarUpload">
      <div v-if="img"  class="update-img fx-h fx-ac fx-bc">
        <img  :src="img" class="avatar">
      </div>
      <div v-if="!img" class="">{{fileName}}</div>

    </el-upload>

    <i v-if="img" class="el-icon-delete" style="position:absolute;background: #fff;top: 0;right: 0;z-index:999" @click="tapDelete()"></i>
  </div>
</template>


<script>
    export default {
        name: "ui-upload",
        computed: {},
        props:{
            upUrl:{
                default:'',
                type:String
            },
            headers:{
                default: ()=> { return {};},
                type:Object
            },
            updata:{
                default: ()=> { return {};},
                type:Object
            },
            img:{
                default:'',
                type:String
            },
            fileName:{
                default:'',
                type:String
            },
            type:{
                default:'img',
                type:String
            }
        },
        data() {
            return {
            }
        },

        created() {
        },
        mounted(){

        },
        methods: {
            beforeAvatarUpload(file) {
                const array = ['image/jpeg','image/png','image/gif'];
                const isLt2M = file.size / 1024 / 1024 < 2;
                var isImg = true;
                if (array.indexOf(file.type) == -1) {
                    this.$message.error('上传头像图片只能支持JPG,PNG,GI格式!');
                    isImg = false;
                }
                if (!isLt2M) {
                    this.$message.error('上传头像图片大小不能超过 2MB!');
                }
                this.$utils.loading(this,"上传中..");
                return isImg && isLt2M;
            },
            /**
             * 上传成功以后
             */
            handleAvatarSuccess(res, file) {
                this.$utils.loadingClose();
                // this.imageUrl = URL.createObjectURL(file.raw);
                this.$emit("onUpload",res);
            },

            handleAvatarError:function (err, file, fileList) {
                this.$utils.loadingClose();
              //  this.$message.error(err);
                console.log(err);
            },
          tapDelete:function () {

             this.$emit('onDelete','');
          }

        }
    }
</script>
<style scoped>
  .avatar-uploader{
    border: 1px dashed #d9d9d9;
    width: 82px;
    height: 82px;
    border-radius: 6px;
    cursor: pointer;
    position: relative;
    overflow: hidden;
  }

  .avatar-uploader .el-upload:hover {
    border-color: #409EFF;
  }
  .avatar-uploader-icon {
    font-size: 28px;
    color: #8c939d;
    width: 80px;
    height: 80px;
    line-height: 80px;
    text-align: center;
  }
  .update-img{ height: 80px; width: 80px; overflow:hidden;}
  .avatar {width: 80px;display: block;}
</style>
